package com.ecoolex.chinadutyfree.retailer.api.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsShoppingCart;
import com.ecoolex.chinadutyfree.retailer.ws.DirectMailGoodsShoppingCartService;
import com.ecoolex.framework.common.annotation.Description;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyType;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyTypeService;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;


@GraphQL2Module(type = Type.User, description = "直邮购物车")
public class DirectMailGoodsShoppingCartGraphql {

	@Reference
	private DirectMailGoodsShoppingCartService service;

	@Autowired
	private StorageService storageService;
	
	@Autowired
	private CurrencyTypeService currencyTypeService;
	
	@TypeField(type = DirectMailGoodsShoppingCart.class, field = "currencyType", description = "币种")
	public CurrencyType currencyType(@Source DirectMailGoodsShoppingCart source) {
		return currencyTypeService.get(source.getCurrencyType());
	}

	@TypeField(type = DirectMailGoodsShoppingCart.class, field = "image", description = "图片")
	public StorageInfo image(@Source DirectMailGoodsShoppingCart source) {
		return storageService.get(source.getImage());
	}

	@MutationField(field = "addDirectMailGoodsShoppingCart", description = "添加直邮商品到购物车")
	public DirectMailGoodsShoppingCart add(@NonNull Integer directMailProductId, @NonNull Integer buyNumber, @Description("是否临时") Boolean isTemporary, ConsumerMemberAuthority user) {
		user.assertLogin();
		return service.addShoppingCart(directMailProductId, buyNumber, user.userId(), user.userName(), isTemporary);
	}

	@MutationField(field = "delDirectMailGoodsShoppingCart", description = "删除直邮购物车中的商品")
	public Boolean del(@NonNull List<Integer> cartIdList, ConsumerMemberAuthority user) {
		user.assertLogin();
		service.delShoppingCart(cartIdList, user.userId(), user.userName());
		return true;
	}

	@MutationField(field = "updateDirectMailGoodsShoppingCart", description = "修改直邮购物车中的商品数量")
	public DirectMailGoodsShoppingCart update(@NonNull Integer cartId, @NonNull Integer buyNumber, ConsumerMemberAuthority user) {
		user.assertLogin();
		return service.updateShoppingCart(cartId, buyNumber, user.userId());
	}

	@QueryField(field = "directMailGoodsShoppingCart", description = "根据id查询直邮购物车详情")
	public DirectMailGoodsShoppingCart getById(@NonNull Integer cartId, ConsumerMemberAuthority user) {
		user.assertLogin();
		return service.getShoppingCartById(cartId, user.userId());
	}

	@QueryField(field = "directMailGoodsShoppingCartList", description = "查询直邮购物车列表")
	public Pagination<DirectMailGoodsShoppingCart> query(@NonNull Sorting[] sorts, @NonNull Page page, ConsumerMemberAuthority user) {
		user.assertLogin();
		return service.queryShoppingCartList(sorts, page, user.userId());
	}

	@QueryField(field = "directMailGoodsShoppingCartListByCartIds", description = "根据购物车Id查询直邮购物车列表")
	public List<DirectMailGoodsShoppingCart> query(@NonNull List<Integer> cartIds, ConsumerMemberAuthority user) {
		user.assertLogin();
		return service.queryShoppingCartByCartIds(cartIds, user.userId());
	}

	@QueryField(field = "directMailGoodsShoppingCartTotalNumber", description = "查询直邮购物车总数量")
	public Long directMailGoodsShoppingCartTotalNumber(ConsumerMemberAuthority user) {
		if (user.isLogin()) {
			return service.countValidTotalNumberByUserId(user.userId());
		} else {
			return 0L;
		}
	}
}
